package cn.itrip.service;

import cn.itrip.beans.vo.hotel.ItripHotelVO;
import cn.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.dao.BaseQuery;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2020/3/16 23:36
 * @Version: 1.0
 */
@Service
public class HotelSearchServiceImpl implements HotelSearchService {

    @Resource
    private BaseQuery<ItripHotelVO> baseQuery;

    @Override
    public Page<ItripHotelVO> getHotelForPage(SearchHotelVO searchHotelVO) throws Exception {

        //创建SolrQuery对象
        SolrQuery solrQuery = new SolrQuery("*:*");

        //下面是拼接 q 条件（目的地、关键词）
        StringBuilder builder = new StringBuilder("");
        builder.append(" destination:"+searchHotelVO.getDestination());
        if(EmptyUtils.isNotEmpty(searchHotelVO.getKeywords())){
            //xxx xxx 注意：如果中间有空格，会忽略掉后面的文字
            searchHotelVO.setKeywords(searchHotelVO.getKeywords().replace(" ","|"));
            builder.append(" AND keyword:"+searchHotelVO.getKeywords());
        }
        solrQuery.setQuery(builder.toString());

        //下面是拼接 fq 条件(位置)

        if(EmptyUtils.isNotEmpty(searchHotelVO.getTradeAreaIds())){
            StringBuilder builder1 = new StringBuilder("(");
            //tradingAreaIds:*,123,* OR tradingAreaIds:*,456,*
            String[] ids = searchHotelVO.getTradeAreaIds().split(",");
            boolean isFirst = true;
            for(String id : ids){
                if(isFirst){
                    builder1.append(" tradingAreaIds:*,"+id+",*");
                    isFirst = false;
                }else{
                    builder1.append(" OR tradingAreaIds:*,"+id+",*");
                }
            }
            builder1.append(")");
            solrQuery.addFilterQuery(builder1.toString());
        }

        //拼接 fq 条件（价格）
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice())){
            //最低价格只要低于客户最高的价格  都满足
            solrQuery.addFilterQuery("minPrice:[* TO "+searchHotelVO.getMaxPrice()+"]");
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            //
            solrQuery.addFilterQuery("minPrice:["+searchHotelVO.getMinPrice()+" TO *");
        }

        //星级
        if(EmptyUtils.isNotEmpty(searchHotelVO.getHotelLevel())){
            solrQuery.addFilterQuery("hotelLevel:"+searchHotelVO.getHotelLevel());
        }
        //功能
        if(EmptyUtils.isNotEmpty(searchHotelVO.getFeatureIds())){
            StringBuilder builder2 = new StringBuilder("(");
            //featureIds:*,123,* OR featureIds:*,456,*
            String[] ids = searchHotelVO.getFeatureIds().split(",");
            boolean isFirst = true;
            for(String id : ids){
                if(isFirst){
                    builder2.append(" featureIds:*,"+id+",*");
                    isFirst = false;
                }else{
                    builder2.append(" OR featureIds:*,"+id+",*");
                }
            }
            builder2.append(")");
            solrQuery.addFilterQuery(builder2.toString());
        }

        //排序
        if(EmptyUtils.isNotEmpty(searchHotelVO.getAscSort())){
            solrQuery.addSort(searchHotelVO.getAscSort(),SolrQuery.ORDER.asc);
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getDescSort())){
            solrQuery.addSort(searchHotelVO.getDescSort(),SolrQuery.ORDER.desc);
        }

        return baseQuery.getHotelByPage(solrQuery,searchHotelVO.getPageNo(),
                searchHotelVO.getPageSize(),ItripHotelVO.class);
    }

    /**
     * 根据热门城市查询
     * @param searchHotCityVO
     * @return
     * @throws Exception
     */
    @Override
    public List<ItripHotelVO> getTopHotelByCityId(SearchHotCityVO searchHotCityVO) throws Exception {

        SolrQuery solrQuery = new SolrQuery("*:*");
        solrQuery.addFilterQuery("cityId:"+searchHotCityVO.getCityId());
        return baseQuery.getTop(solrQuery,searchHotCityVO.getCount(),ItripHotelVO.class);
    }
}
